package com.fyy.netty.chapter02.c1;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import lombok.extern.slf4j.Slf4j;

import java.net.InetAddress;
import java.nio.charset.Charset;

/**
 * @Author fyy
 * @Description
 * @Date 2021/4/26 14:26
 */
@Slf4j
public class EventLoopServer {

    public static void main(String[] args) {
        //主服务，只处理ServerSocketChannel的连接事件
        EventLoopGroup boss = new NioEventLoopGroup(1);
        //工作服务，只处理SocketChannel的读、写事件
        EventLoopGroup worker = new NioEventLoopGroup(10);
        //自定义处理组
        EventLoopGroup group = new DefaultEventLoop();
        new ServerBootstrap()
                .group(boss,worker)
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel sc) throws Exception {
                        sc.pipeline().addLast("worker1",new ChannelInboundHandlerAdapter(){
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                ByteBuf buf = (ByteBuf) msg;
                                log.info(buf.toString(Charset.forName("UTF-8")));
                            }
                        });
                    }
                })
                .bind(8080);
    }

}
